package io.chenglicun.modules.clc.service.dataset.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.clc.dao.dataset.ClcDataColumnDao;
import io.chenglicun.modules.clc.dao.dataset.ClcDataSetColumnDao;
import io.chenglicun.modules.clc.dto.dataset.ClcDataColumnDTO;
import io.chenglicun.modules.clc.dto.dataset.ClcDataContentDTO;
import io.chenglicun.modules.clc.dto.dataset.ClcDataSetColumnDTO;
import io.chenglicun.modules.clc.entity.dataset.ClcDataColumnEntity;
import io.chenglicun.modules.clc.entity.dataset.ClcDataContentEntity;
import io.chenglicun.modules.clc.entity.dataset.ClcDataSetColumnEntity;
import io.chenglicun.modules.clc.excel.dataset.ClcDataColumnImportExcel;
import io.chenglicun.modules.clc.service.dataset.ClcDataColumnService;
import io.chenglicun.modules.clc.service.dataset.ClcDataContentService;
import io.chenglicun.modules.clc.service.dataset.ClcDataSetColumnService;
import io.chenglicun.modules.security.user.SecurityUser;
import io.chenglicun.modules.sys.dto.SysDictDataDTO;
import io.chenglicun.modules.sys.service.SysDictDataService;
import io.chenglicun.modules.sys.service.SysDictTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据字段
 *
 * @author HFHQ 1986866856@qq.com
 * @since 1.0 2024-02-26
 */
@Service
public class ClcDataColumnServiceImpl extends CrudServiceImpl<ClcDataColumnDao, ClcDataColumnEntity, ClcDataColumnDTO> implements ClcDataColumnService {


    @Autowired
    private ClcDataContentService clcDataContentService;
    @Override
    public QueryWrapper<ClcDataColumnEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcDataColumnEntity> wrapper = new QueryWrapper<>();

        Object keyStr = params.get("keyStr");
        if (ObjectUtil.isNotEmpty(keyStr)){
            wrapper.like( "name", keyStr)
                    .or()
                    .like("ch_initials", keyStr)
                    .or()
                    .like("in_id",keyStr);
        }

        return wrapper;
    }

    @Autowired
    ClcDataSetColumnService clcDataSetColumnService;
    @Override
    public List<ClcDataColumnDTO> listbyset(Map<String, Object> params) {
        List<ClcDataSetColumnDTO> setColumnDTOS = clcDataSetColumnService.list(params);
        List<Long> columnIds = new ArrayList<>();
        for (ClcDataSetColumnDTO setColumnDTO : setColumnDTOS) {
            columnIds.add(setColumnDTO.getColumnId());
        }
        List<ClcDataColumnDTO> clcDataColumnDTOS = new ArrayList<>();
        if(columnIds.size()<=0)
            return clcDataColumnDTOS;
        List<ClcDataColumnEntity> clcDataColumnEntities = baseDao.selectBatchIds(columnIds);
//        Map<Long, List<ClcDataContentEntity>> contentMap = SimpleQuery.group(new LambdaQueryWrapper<ClcDataContentEntity>().in(ClcDataContentEntity::getColumnId, columnIds), ClcDataContentEntity::getColumnId);
        clcDataColumnDTOS = ConvertUtils.sourceToTarget(clcDataColumnEntities, ClcDataColumnDTO.class);
        clcDataColumnDTOS.forEach(e -> {
//            List<ClcDataContentEntity> contents = contentMap.get(e.getId());
//            if (CollectionUtils.isNotEmpty(contents)){
//                List<ClcDataContentDTO> clcDataContentDTOS = ConvertUtils.sourceToTarget(contents, ClcDataContentDTO.class);
//                e.setDataContentDTOList(clcDataContentDTOS);
//            }
        });
        return clcDataColumnDTOS;
    }


    @Autowired
    SysDictTypeService sysDictTypeService;

    @Autowired
    SysDictDataService sysDictDataService;

    @Override
    public void importExcel(List<ClcDataColumnImportExcel> list) {

        //数据分类
        List<SysDictDataDTO> dataTypeList = sysDictDataService.getDictDataList("data_type");

        //数据等级
        List<SysDictDataDTO> dataLevelList = sysDictDataService.getDictDataList("data_level");

        //数据共享
        List<SysDictDataDTO> dataShareList = sysDictDataService.getDictDataList("data_share");

        //数据开放
        List<SysDictDataDTO> dataOpenList = sysDictDataService.getDictDataList("data_open");

        for (ClcDataColumnImportExcel excel : list) {
            ClcDataColumnEntity clcDataColumnEntity = new ClcDataColumnEntity();
            clcDataColumnEntity.setName(excel.getName());
            clcDataColumnEntity.setChInitials(excel.getChInitials());
            clcDataColumnEntity.setInId(excel.getInId());

            //从字典表里获取数据
            //数据分类
            clcDataColumnEntity.setDataType(getDataValue(dataTypeList, excel.getDataType()));

            //数据等级
            clcDataColumnEntity.setDataLevel(getDataValue(dataLevelList, excel.getDataLevel()));

            //数据共享
            clcDataColumnEntity.setDataShare(getDataValue(dataShareList, excel.getDataShare()));

            //数据开放
            clcDataColumnEntity.setDataOpen(getDataValue(dataOpenList, excel.getDataOpen()));

            this.baseDao.insert(clcDataColumnEntity);
        }

    }

    private Integer getDataValue(List<SysDictDataDTO> dataValueList, String dataLabel) {
        String dataValueStr =  dataValueList.stream()
                .filter(sysDictDataDTO -> sysDictDataDTO.getDictLabel().equals(dataLabel))
                .map(SysDictDataDTO::getDictValue)
                .findFirst().orElse(null);
        if(dataValueStr == null) {
            return null;
        }
        return Integer.parseInt(dataValueStr);
    }
}
